package com.keyroy.game.puzzle_rpg.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

public class IconView extends Canvas {
	// ff7f50
	private static final Color COLOR = new Color(null, 0xff, 0x7f, 0x50);
	private Image image;

	public IconView(Composite parent) {
		super(parent, SWT.BORDER | SWT.DOUBLE_BUFFERED);

		addPaintListener(new PaintListener() {

			@Override
			public void paintControl(PaintEvent e) {
				if (image != null) {

					Rectangle rectangle = image.getBounds();
					int x = (e.width - rectangle.width) / 2;
					int y = (e.height - rectangle.height) / 2;
					e.gc.drawImage(image, x, y);

					e.gc.setForeground(COLOR);
					e.gc.setBackground(COLOR);
					e.gc.drawString(rectangle.width + "x" + rectangle.height, 0, 0, true);
					e.gc.drawRectangle(x, y, rectangle.width, rectangle.height);

				}
			}
		});
	}

	public void setImage(String filePath) {
		try {
			image = new Image(null, filePath);
		} catch (Exception e) {
		}
		redraw();
	}

	public Image getImage() {
		return image;
	}

	public int imageWidth() {
		return image.getBounds().width;
	}

	public int imageHeight() {
		return image.getBounds().height;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
